package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCargoPublishPersonBinding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.transport.CargoPublishEntity;
import com.sgb.kjwl.model.entity.transport.SingleItemConstant;
import com.sgb.kjwl.utils.DefaultUtil;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.widget.transport.AMapDialog;
import com.sgb.kjwl.viewmodel.transport.CargoPublishPersonViewModel;
import com.sgb.link.entity.AMapAddressEntity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.MToast;

import javax.inject.Inject;

/**
 * @author: 杨畔
 * @date: 2020/12/29 17:50
 * @Description: 货源发布选择发货人/收货人页面
 */
public class CargoPublishPersonActivity extends AppActivity {

    @Inject
    CargoPublishPersonViewModel viewModel;
    ActivityCargoPublishPersonBinding binding;
    //数据实体类
    private CargoPublishEntity.LocationEntity mEntity;
    //当前类型
    private int mType;//0:发货人head 1:发货人sub 2:收货人sub 3:收货人footer

    public static void start(Activity activity, CargoPublishEntity.LocationEntity entity, int requestCode) {
        Intent intent = new Intent(activity, CargoPublishPersonActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra(RouterKey.DATA, entity);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent.builder()
                .appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_cargo_publish_person);
        binding.setLifecycleOwner(this);
        initEvent();
        initData();
    }

    private void initEvent() {
        binding.setOnClick(this);
        binding.includeTitle.ivBack.setOnClickListener(this);
    }

    private void initData() {
        mEntity = getIntent().getParcelableExtra(RouterKey.DATA);
        if (mEntity == null) {
            mEntity = new CargoPublishEntity.LocationEntity();
        }
        //同步数据
        syncData();
    }

    private void syncData() {
        mType = mEntity.getItemType();
        //设置发货/收货对应页面信息
        if (mType == SingleItemConstant.TYPE_ONE || mType == SingleItemConstant.TYPE_TWO) {
            binding.includeTitle.titleView.setText("发货人信息");
            binding.tvReleaseCargoLocationTip.setContentText("发货地");
            binding.tvReleaseCargoLocation.setHint("请选择发货地址");
            binding.tvReleaseCargoAddressTip.setText("详细发货地");
            binding.tvReleaseCargoUnitTip.setText("发货单位");
            binding.tvReleaseCargoContactTip.setContentText("装车对接人");
            binding.etReleaseCargoContact.setHint("请输入装车对接人");
            binding.tvReleaseCargoTip.setText("在您未确认运输订单前，车主无法查看发货人和联系电话，请放心填写");
        } else {
            binding.includeTitle.titleView.setText("收货人信息");
            binding.tvReleaseCargoLocationTip.setContentText("收货地");
            binding.tvReleaseCargoLocation.setHint("请选择收货地址");
            binding.tvReleaseCargoAddressTip.setText("详细收货地");
            binding.tvReleaseCargoUnitTip.setText("收货单位");
            binding.tvReleaseCargoContactTip.setContentText("卸车对接人");
            binding.etReleaseCargoContact.setHint("请输入卸车对接人");
            binding.tvReleaseCargoTip.setText("在您未确认运输订单前，车主无法查看收货人和联系电话，请放心填写");
        }
        //设置地址
        if (!TextUtils.isEmpty(mEntity.getProvince())) {
            binding.tvReleaseCargoLocation.setText(String.format("%s%s%s",
                    mEntity.getProvince(),
                    mEntity.getCity(),
                    mEntity.getDistrict()));
        }
        if (!TextUtils.isEmpty(mEntity.getAddress())) {
            binding.etReleaseCargoAddress.setText(DefaultUtil.getString(mEntity.getAddress()));
        }
        //设置联系人
        if (mType == SingleItemConstant.TYPE_ONE || mType == SingleItemConstant.TYPE_FOUR) {
            binding.llReleaseCargoContact.setVisibility(View.VISIBLE);
            //货物单位
            binding.etReleaseCargoUnit.setText(DefaultUtil.getString(mEntity.getGoodsUnit()));
            binding.etReleaseCargoContact.setText(DefaultUtil.getString(mEntity.getContact()));
            binding.etReleaseCargoContactTel.setText(DefaultUtil.getString(mEntity.getTelPhone()));
        } else {
            binding.llReleaseCargoContact.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        closeKeybord();
        if (v == binding.includeTitle.ivBack) {
            finish();
        } else if (v == binding.llReleaseCargoLocation) {
            AMapDialog.launch(this);
        } else if (v == binding.tvReleaseCargoSubmit) {
            checkData();
        }
    }

    /**
     * 检查数据并回传
     */
    private void checkData() {
        if (mEntity == null) return;
        if (mEntity.getLongitude() == 0 || mEntity.getLatitude() == 0) {
            MToast.showToast(this, binding.tvReleaseCargoLocation.getHint().toString());
            return;
        }
        //设置地址
        if (binding.etReleaseCargoAddress.getText() != null) {
            mEntity.setAddress(binding.etReleaseCargoAddress.getText().toString());
        } else {
            mEntity.setAddress("");
        }
        //判断联系人
        if (mType == SingleItemConstant.TYPE_ONE || mType == SingleItemConstant.TYPE_FOUR) {
            //设置单位
            if (binding.etReleaseCargoUnit.getText() != null) {
                mEntity.setGoodsUnit(DefaultUtil.getString(binding.etReleaseCargoUnit.getText().toString()));
            } else {
                mEntity.setGoodsUnit(null);
            }
            //联系人
            String contact = binding.etReleaseCargoContact.getText().toString();
            String tel = binding.etReleaseCargoContactTel.getText().toString();
            if(TextUtils.isEmpty(contact)){
                if (mType == SingleItemConstant.TYPE_ONE || mType == SingleItemConstant.TYPE_TWO) {
                    MToast.showToast(this, "请输入装车对接人");
                }else{
                    MToast.showToast(this, "请输入卸车对接人");
                }
                return;
            }
            if(TextUtils.isEmpty(tel)){
                MToast.showToast(this, "请输入联系人电话");
                return;
            }
            if (!TextUtils.isEmpty(tel) && !CheckUtils.isPhoneNumberValid(tel)) {
                MToast.showToast(this, "请填写正确的联系电话");
                return;
            }
            //设置联系人/联系人电话
            mEntity.setContact(DefaultUtil.getString(contact));
            mEntity.setTelPhone(DefaultUtil.getString(tel));
        }
        Intent intent = new Intent();
        intent.putExtra(RouterKey.DATA, mEntity);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;
        AMapAddressEntity entity = AMapDialog.get(data);
        if (entity != null) {
            binding.tvReleaseCargoLocation.setText(String.format("%s%s%s",
                    entity.getProvince(),
                    entity.getCity(),
                    entity.getDistrict()));
            binding.etReleaseCargoAddress.setText(DefaultUtil.getString(entity.getSnippet()));
            //设置数据
            mEntity.setLatitude(entity.getLatitude());
            mEntity.setLongitude(entity.getLongitude());
            mEntity.setProvince(entity.getProvince());
            mEntity.setCity(entity.getCity());
            mEntity.setDistrict(entity.getDistrict());
            mEntity.setProvinceNo(entity.getProvinceNo());
            mEntity.setCityNo(entity.getCityNo());
            mEntity.setDistrictNo(entity.getDistrictNo());
            mEntity.setAddress(entity.getSnippet());
        }
    }
}
